package gwtmodule1.client.gui;

import gwtmodule1.client.gui.shop.PageCategory;
import gwtmodule1.client.gui.shop.PageConfirm;
import gwtmodule1.client.gui.shop.PageDeliver;
import gwtmodule1.client.gui.shop.PageGiftCards;
import gwtmodule1.client.gui.shop.PageMain;
import gwtmodule1.client.gui.shop.PageSearch;
import gwtmodule1.client.gui.shop.PageShoppingCart;
import gwtmodule1.client.gui.shop.PageSubCategory;
import gwtmodule1.client.gui.utils.FrameClient;
import gwtmodule1.client.gui.utils.FrameProvider;
import gwtmodule1.client.gui.utils.VSpacer;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PageShop extends HorizontalPanel implements FrameClient,
		FrameProvider {

	public class BasicCategoryItem extends TreeItem implements ClickListener {

		private FrameClient page;

		private String name;

		private BasicCategoryItem parent;

		public BasicCategoryItem(BasicCategoryItem parent, String name,
				FrameClient page) {
			super();
			this.page = page;
			this.name = name;
			this.parent = parent;
			Hyperlink link = new Hyperlink(name, "Cat" + getName(""));
			link.addClickListener(this);
			setWidget(link);
			if (parent != null)
				parent.addItem(this);
		}

		public String getName(String spacer) {
			if (parent == null)
				return name;
			return parent.name + spacer + name;
		}

		public void onClick(Widget sender) {
			page.onShow(this);
			center.setWidget((Widget) page);
		}
	}

	private class CategoryItem extends BasicCategoryItem {

		public CategoryItem(BasicCategoryItem parent, String name,
				FrameClient page) {
			super(parent, name, page);
			setStyleName(STYLE_CATEGORY_ITEM);
		}

	}

	private class SubCategoryItem extends BasicCategoryItem {

		public SubCategoryItem(BasicCategoryItem parent, String name,
				FrameClient page) {
			super(parent, name, page);
			setStyleName(STYLE_SUBCATEGORY_ITEM);
		}

	}

	public final static String STYLE_CATEGORY_ITEM = "CategoryItem";

	public final static String STYLE_SUBCATEGORY_ITEM = "SubCategoryItem";

	private PageConfirm pageConfirm = new PageConfirm(this);

	private PageDeliver pageDeliver = new PageDeliver(this);

	private PageShoppingCart pageShoppingCart = new PageShoppingCart(this);

	private PageCategory pageCategory = new PageCategory();

	private PageSubCategory pageSubCategory = new PageSubCategory();

	private PageSearch pageSearch = new PageSearch();

	private PageMain pageMain = new PageMain();

	private PageGiftCards pageGiftCards = new PageGiftCards();

	private SimplePanel center;

	public PageShop() {
		super();
		add(createLeft());
		add(new VSpacer("32px"));
		add(createCenter());
	}

	private Widget createCenter() {
		center = new SimplePanel();
		center.setWidth("100%");
		center.setWidget(pageMain);
		return center;
	}

	private Widget createLeft() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(createShoppingCart());

		Hyperlink products = new Hyperlink("<br><b>Products</b>", true,
				"ShopMain");
		products.addClickListener(getPageSwitch(pageMain));
		panel.add(products);
		panel.add(createTree());
		return panel;
	}

	private Widget createShoppingCart() {
		VerticalPanel panel = new VerticalPanel();
		Hyperlink cart = new Hyperlink("<b>Shopping Cart</b>", true,
				"ShoppingCart");
		cart.addClickListener(getPageSwitch(pageShoppingCart));
		panel.add(cart);
		panel.add(new HTML("4 Items; 639.05 USD"));
		panel.setStyleName("MiniShoppingCart");
		return panel;
	}

	private Widget createTree() {
		Tree tree = new Tree();

		CategoryItem search = new CategoryItem(null, "Search", pageSearch);

		CategoryItem elec = new CategoryItem(null, "Electronic", pageCategory);
		new SubCategoryItem(elec, "TV", pageSubCategory);
		new SubCategoryItem(elec, "DVD", pageSubCategory);
		new SubCategoryItem(elec, "MP3", pageSubCategory);
		new SubCategoryItem(elec, "CD", pageSubCategory);

		CategoryItem movies = new CategoryItem(null, "Movies", pageCategory);
		new SubCategoryItem(movies, "Horror", pageSubCategory);
		new SubCategoryItem(movies, "Scifi", pageSubCategory);
		new SubCategoryItem(movies, "Drama", pageSubCategory);
		new SubCategoryItem(movies, "Doku", pageSubCategory);

		CategoryItem giftcards = new CategoryItem(null, "Gift Cards",
				pageGiftCards);

		tree.addItem(search);
		tree.addItem(elec);
		tree.addItem(movies);
		tree.addItem(giftcards);
		return tree;
	}

	public SimplePanel getFrame() {
		return center;
	}

	public PageConfirm getPageConfirm() {
		return pageConfirm;
	}

	public PageDeliver getPageDeliver() {
		return pageDeliver;
	}

	public ClickListener getPageSwitch(final Widget page) {
		return new ClickListener() {
			public void onClick(Widget sender) {
				center.setWidget(page);
			}
		};
	}

	public void onShow(Object param) {
	}

}
